<template>
  <div class="saleInfoMaintenance">
    <h3 class="titleH3">经销商归属</h3>
    <FormPanel
        :list="FormPanelList0"
        :cols="1"
        :labelWidth="120"
        :isSubmitBtn='false'
        :formType="type == 'edit'?'show':type"
        ref="FormPanel"
        :collapse="false"
      ></FormPanel>
      <h3 class="titleH3">基本信息</h3>
      <FormPanel
        :list="FormPanelList"
        :cols="3"
        :labelWidth="120"
        :isSubmitBtn='false'
        :formType="type"
        ref="FormPanel1"
        :collapse="false"
      ></FormPanel>
     
      <div :style="{
          position: 'absolute',
          left: 0,
          bottom: 0,
          right: 0,
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'center'
        }">
          <el-button size="small" type=""  @click="closeHandler">返回</el-button>
          <el-button size="small" v-show="type == 'show'?false:true" type="primary"  @click="saveHandle">保存</el-button>
      </div>

      <!-- <BaseDialog 
      :visible.sync="visible" 
      title="新增授权"
      width="30%"
      destroyOnClose>
        <FormPanel
          :list="FormPanelList2"
          :cols="1"
          :labelWidth="120"
          :isSubmitBtn='false'
          formType="edit"
          :collapse="false"
        ></FormPanel>
        <div style="text-align: center;margin: 20px 0 20px 0;">
          <el-button size="small" type=""  @click="cancal">返回</el-button>
          <el-button size="small" v-show="type == 'show'?false:true" type="primary"  @click="addSaveHandle">保存</el-button>
      </div>
    </BaseDialog> -->
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import {getAfterSaleInfo,updateAfterSale,addSaleInfoMaintain,insertAfterSaleInfo,getMerge,getOrgByOrgType} from '@/api/common/CompanyRepair/saleInfoMaintenance';
import * as types from '@/api/common/auth/roleManage/types';
import {
getDistributors,
getDistributorALL
} from '@/api/common/CompanySale/dealerInfoMaintenance';

export default {
  name: 'modal',
  mixins: [dictionary],
  components: {
  },
  props:['rowData','type','listAllData'],
  data() {
    this.BaseTable = null;
    return {
        FormPanelList0:this.createFormPanelList0(),
        FormPanelList: this.createFormPanelList(),
        FormPanelList2:this.createFormPanelList2(),
        dataSource:[{ billType: '1', brandId: '1' }],
        isShow: true,
        visible:false,
        rowDataTable:'',
        province:[],
        dialogTitle:'',
        isHidden:70401002,
        saveType:"",
    };
  },
  methods: {
      createFormPanelList0() {
        return [
          {
            type: 'SELECT',
            label: '所属经销商公司',
            fieldName: 'companyId',
            placeholder: '',
            style: { width: '15.6vw' },
            itemList: [],
            rules: [{ required: true, message: '请选择所属经销商公司', trigger: 'change' }]
          }
        ];
      },
      createFormPanelList() {
      return [
        {
          type: 'INPUT',
          label: '维修站代码',
          fieldName: 'ascCode',
          placeholder: '',
          style: { width: '15.6vw' },
          disabled:false,
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '维修站名称',
          fieldName: 'ascName',
          placeholder: '',
          disabled:false,
          style: { width: '15.6vw' },
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '维修站简称',
          fieldName: 'ascShortName',
          placeholder: '',
          style: { width: '15.6vw' },
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT_TREE',
          label: '售后区域',
          fieldName: 'isColdArea',
          placeholder: '',
          style: { width: '15.6vw' },
          itemList: [],
          rules: [{ required: true, message: '请选择市', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '省',
          fieldName: 'province',
          placeholder: '',
          style: { width: '15.6vw' },
          itemList: [],
          change:val=>{
            let city=[]
            if(val == 3662){
              this.FormPanelList[5].itemList=[{text:"台湾",value:3662}]
              this.FormPanelList[6].itemList=[{text:"台湾",value:3662}]
            }else if(val == 3663){
              this.FormPanelList[5].itemList=[{text:"香港",value:3663}]
              this.FormPanelList[6].itemList=[{text:"香港",value:3663}]
            }else if(val == 3664){
              this.FormPanelList[5].itemList=[{text:"澳门",value:3664}]
              this.FormPanelList[6].itemList=[{text:"澳门",value:3664}]
            }
            this.FormPanelList[5].initialValue=''
            this.FormPanelList[6].initialValue=''
            this.listAllData.forEach(x=>{
              // 筛选省
              if(x.parentRegionId == val){
                city.push({text:x.regionName,value:x.regionId})
                this.FormPanelList[5].itemList=_.uniqBy(city,'value')
              }
            })
          },
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '市',
          fieldName: 'city',
          placeholder: '',
          style: { width: '15.6vw' },
          itemList: [],
          change:val=>{
            let county=[]
            this.FormPanelList[6].initialValue=''
            this.listAllData.forEach(x=>{
              // 筛选省
              if(x.parentRegionId == val){
                county.push({text:x.regionName,value:x.regionId})
                this.FormPanelList[6].itemList=_.uniqBy(county,'value')
              }
            })
          },
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '区/县',
          fieldName: 'county',
          placeholder: '',
          style: { width: '15.6vw' },
          itemList: [],
        },
        {
          type: 'INPUT',
          label: '地址',
          fieldName: 'address',
          placeholder: '',
          style: { width: '15.6vw' },
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '开户银行',
          fieldName: 'bank',
          placeholder: '',
          style: { width: '15.6vw' },
          itemList: [],
        },
        {
          type: 'INPUT',
          label: '开户账号',
          fieldName: 'bankAccount',
          placeholder: '',
          style: { width: '15.6vw' },
        },
        {
          type: 'INPUT',
          label: '税号',
          fieldName: 'dutyNumber',
          placeholder: '',
          style: { width: '15.6vw' },
        },
        {
          type: 'SELECT',
          label: '营业状态',
          fieldName: 'status',
          placeholder: '',
          style: { width: '15.6vw' },
          itemList: this.createDictList('1011'),
          rules: [{ required: true, message: '请选择市', trigger: 'change' }]
        },
      ];
    },
    createFormPanelList2() {
        return [
          {
            type: 'SELECT',
            label: '授权类型',
            fieldName: 'billType',
            placeholder: '',
            style: { width: '15.6vw' },
            itemList: [{ text: '品牌', value: '70401001' }, { text: '车系', value: '70401002' }],
            change:val=>{
              // this.isHidden=val
              if(val == 70401002){
                this.FormPanelList2[2].hidden=false
                this.FormPanelList2[0].initialValue="车系"
              }else{
                this.FormPanelList2[2].hidden=true
                this.FormPanelList2[0].initialValue="品牌"
              }
            },
            rules: [{ required: true, message: '请选择授权类型', trigger: 'change' }]
          },
          {
            type: 'SELECT',
            label: '授权品牌',
            fieldName: 'brandId',
            placeholder: '',
            style: { width: '15.6vw' },
            itemList: this.createDictList('7040'),
            rules: [{ required: true, message: '请选择授权品牌', trigger: 'change' }]
          },
          {
            type: 'SELECT',
            label: '授权车系',
            fieldName: 'seriesId',
            placeholder: '',
            hidden:true,
            style: { width: '15.6vw' },
            itemList: this.createDictList('7040'),
            rules: [{ required: true, message: '请选择授权车系', trigger: 'change' }]
          }
        ];
      },
    
    init(){
        if(this.type == 'edit'){
            this.FormPanelList[0].disabled=true
            this.FormPanelList[1].disabled=true
            this.FormPanelList0.find(x=>x.fieldName == 'companyId').itemList=[{ text: this.rowData.companyName, value: this.rowData.companyId}]
            /* getDistributorALL().then(res=>{
              console.log(res,"edit")
              res.data.forEach(x=>{
                this.FormPanelList0.find(x=>x.fieldName == 'companyId').itemList.push({ text: x.COMPANY_NAME_CN, value: x.COMPANY_ID })
              })
            }) */
        }else{
            this.FormPanelList[0].disabled=false
            this.FormPanelList[1].disabled=false
            getDistributors().then(res=>{
            res.data.forEach(x=>{
              console.log(res,"新增")
              this.FormPanelList0[0].itemList.push({ text: x.COMPANY_NAME_CN, value: x.COMPANY_ID })
            })
          })
        }

        if(this.type === 'show'){
          this.FormPanelList0.find(x=>x.fieldName == 'companyId').itemList=[{ text: this.rowData.companyName, value: this.rowData.companyId}]
          /* getDistributorALL().then(res=>{
              console.log(res,"show")
              res.data.forEach(x=>{
                this.FormPanelList0.find(x=>x.fieldName == 'companyId').itemList.push({ text: x.COMPANY_NAME_CN, value: x.COMPANY_ID })
              })
            }) */
        }
        
        this.FormPanelList0[0].initialValue=this.rowData.companyId
        this.FormPanelList.forEach(ele=>{
            ele.initialValue=this.rowData[ele.fieldName]
        })
        if(this.rowData['status']){
          this.FormPanelList.find(x=>x.fieldName == "status").initialValue = Number(this.rowData['status'])
        }
        
        //初始化省市区赋值
        let initCity=[]
        let initCounty=[]
        this.listAllData.forEach(x=>{
            // 筛选省
            if(x.parentRegionId == null){
            this.province.push({text:x.regionName,value:`${x.regionId}`})
            }

            // 筛选市
            if(x.parentRegionId == this.FormPanelList[4].initialValue){
              initCity.push({text:x.regionName,value:`${x.regionId}`})
            }

            // 筛选区
            if(x.parentRegionId == this.FormPanelList[5].initialValue){
              initCounty.push({text:x.regionName,value:`${x.regionId}`})
            }
        })
        console.log(this.FormPanelList[4],"this.FormPanelList[4]")
        this.FormPanelList.find(item => item.fieldName === 'province').itemList=_.uniqBy(this.province,'value')
        this.FormPanelList.find(item => item.fieldName === 'city').itemList=_.uniqBy(initCity,'value')
        this.FormPanelList.find(item => item.fieldName === 'county').itemList=_.uniqBy(initCounty,'value')
        //  this.FormPanelList[4].itemList=_.uniqBy(this.province,'value')
        // this.FormPanelList[5].itemList=_.uniqBy(initCity,'value')
        // this.FormPanelList[6].itemList=_.uniqBy(initCounty,'value')
        if(this.type == 'add'){
          // this.FormPanelList[5].itemList=[]
          // this.FormPanelList[6].itemList=[]
          this.FormPanelList.find(item => item.fieldName === 'city').itemList=[]
          this.FormPanelList.find(item => item.fieldName === 'county').itemList=[]
        }
        this.getArea()
        // insertAfterSaleInfo().then(res=>{
        //   console.log(res,"售后区域")
        // })
        console.log(this.FormPanelList.find(item => item.fieldName === 'isColdArea'),"售后区域456")
        this.FormPanelList.find(item => item.fieldName === 'isColdArea').initialValue = String(this.FormPanelList.find(item => item.fieldName === 'isColdArea').initialValue)
    },
    //获取所属区域
    getArea(companyId){
      getMerge().then(res=>{
        console.log(res,"res")
        this.FormPanelList.find(item => item.fieldName === 'isColdArea').itemList=[]
        if(res.data.isMerge == 10041001){
            if(res.data.isDisArea == 10041001){
              // 销售大区15061005
              getOrgByOrgType(15061005).then(res=>{
                console.log(res,"销售大区15061005")
                res.data.forEach(x=>{
                  this.FormPanelList.find(item => item.fieldName === 'isColdArea').itemList.push({ text: x.orgName, value: `${x.orgId}` })
                })
              })
            }else{
              //销售小区15061007
              getOrgByOrgType(15061007).then(res=>{
                console.log(res,"销售小区15061007")
                res.data.forEach(x=>{
                  this.FormPanelList.find(item => item.fieldName === 'isColdArea').itemList.push({ text: x.orgName, value: `${x.orgId}` })
                })
              })
            } 
        }else{
            if(res.data.isDisArea == 10041001){
              // 售后大区15061006
              getOrgByOrgType(15061006).then(res=>{
                console.log(res,"售后大区15061006")
                res.data.forEach(x=>{
                  this.FormPanelList.find(item => item.fieldName === 'isColdArea').itemList.push({ text: x.orgName, value: `${x.orgId}` })
                })
              })
            }else{
              //售后小区15061008
              getOrgByOrgType(15061008).then(res=>{
                console.log(res,"售后小区15061008")
                res.data.forEach(x=>{
                  this.FormPanelList.find(item => item.fieldName === 'isColdArea').itemList.push({ text: x.orgName, value: `${x.orgId}` })
                })
              })
            } 
        }
      })
    },
    getCookie(){
        let allCookie=document.cookie.split(';')
        allCookie.forEach(x=>{
          if(x.indexOf('dataType') > -1 ){
            this.dataType=x.split('=')[1]
            }
        })
        //判断是否为车厂10461003
        console.log(this.dataType,"dataType")
        if(this.dataType == 10461003){
          console.log(this.dataType,"车厂")
        }
      },
    // 新增
    addTable(){
        this.visible=true
    },
   
    // 保存
    saveHandle(){
       this.$refs.FormPanel.SUBMIT_FORM()
      let FormPanelData= this.$refs.FormPanel1.SUBMIT_FORM()
      if(this.FormPanelList0[0].initialValue && this.FormPanelList[0].initialValue && this.FormPanelList[1].initialValue && this.FormPanelList[2].initialValue && this.FormPanelList[3].initialValue&&this.FormPanelList[3].initialValue != null && this.FormPanelList[4].initialValue &&this.FormPanelList[5].initialValue && this.FormPanelList[7].initialValue&& this.FormPanelList[11].initialValue){
       let saveData={
         companyId:this.FormPanelList0[0].initialValue,
         ...FormPanelData,
       }
        console.log(FormPanelData,this.saveType,"新增")
        if(this.type == 'edit'){
            saveData.ascId = this.rowData.ascId
            updateAfterSale(saveData).then(res=>{
            if(res.resultCode == 200){
              this.$notify.success({title: '提示信息',message:"保存成功"})
              this.closeHandler('refresh')
            }else{
              this.$notify.error({title: '提示信息',message:res.errMsg})
            }
          })
        }else{
          addSaleInfoMaintain(saveData).then(res=>{
            if(res.resultCode == 200){
              this.$notify.success({title: '提示信息',message:"保存成功"})
              this.closeHandler('refresh')
            }else{
              this.$notify.error({title: '提示信息',message:res.errMsg})
            }
          })
        }
      }else{
        console.log(456)
      }
    },
    //新增保存
    addSaveHandle(){
      console.log(this.FormPanelList2)
    },
    // 新增返回
    cancal(){
      this.visible=false
    },
    closeHandler(type) {
        this.$emit('close',type)
    }
  },
  mounted() {
    this.getCookie()
    this.init()
  }
};
</script>

<style lang="less" >
.saleInfoMaintenance .titleH3{
    height: 30px;
    line-height: 30px;
    margin-bottom: 5px;
    background: #eee;
    text-indent: 5px;
}
.saleInfoMaintenance .el-form-item__content{
     width: 15.6vw;
}
</style>
